package com.ljm.dao;/**
 * @author： li
 * @date： 2022-04-20 09:07
 * @version 1.0
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ljm.bin.article;
import com.ljm.util.DBUtil1;
/**
 * @author： ljm
 * @date： 2022-04-20 09:07
 * @version 1.0
 */
public class articleDao {
    public static List<article> getArticle() throws SQLException {
        Connection con = DBUtil1.getConnection();
        List<article> ans = new ArrayList<>();
        String sql = "select * from article";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs =ps.executeQuery();
        while(rs.next()){
            String title=rs.getString("title");
            String content=rs.getString("content");
            int id =rs.getInt("id");
            String date=rs.getString("date");
            String img=rs.getString("img");
           //控制台输出
//          System.out.println("title:"+title+"\n"+"content:"+content);
            article a =new article(title,content,id,date,img);
            ans.add(a);
        }
        DBUtil1.close();
        return ans;
    }
    public static ResultSet selectById(String id) throws SQLException {
        Connection con = DBUtil1.getConnection();
        String sql = "select * from article where ID = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1,id);
        ResultSet rs =ps.executeQuery();
        DBUtil1.close();
        return rs;
    }

//    public static void main(String[] args) throws SQLException {
////        List<article> b = new ArrayList<>();
////        b=getArticle();
//            ResultSet a = selectById("1");
//            while (a.next()){
//                System.out.println(a.getString("title")+" "+
//                        a.getString("content"));
//            }
//
//    }
}
